﻿using System;
using System.Xml;
using SdmxMl.Common;
using SdmxMl.GenericData;
using System.Windows.Forms;

namespace SdmxMl.Manager
{
    public class DataSetManager : ArtefactManager
    {
        private DataSetTypeList _dataSetList;
        public DataSetTypeList DataSetList
        {
            get { return _dataSetList; }
        }

        public override void Clear()
        {
            _dataSetList.Clear();
        }

        public override int Count
        {
            get { return _dataSetList.Count; }
        }

        public DataSetManager(SdmxArtefactType aType)
            : base(aType)
        {
            if (artefactType != SdmxArtefactType.DataSet)
                throw new ApplicationException("Unexpected. Invalid construction for dataSet Manager");
            _dataSetList = new DataSetTypeList();
        }


        public override Artefact CreateArtefact()
        {
            // No artefact representation
            return null;
        }
        public override Artefact CreateArtefact(XmlNode nd, SdmxNamespaceManager nsmgr)
        {
            // No artefact representation
            return null;
        }

        public override bool LoadFromDocument(XmlDocument xDoc, SdmxNamespaceManager nsmgr)
        {
            bool found = false;
     //       XmlNodeList nodeList = xDoc.SelectNodes(string.Format("//GenericData:DataSet"), nsmgr);
            XmlNodeList nodeList = xDoc.SelectNodes(string.Format("//S:{0}", artefactType.ToString()), nsmgr);

            if (nodeList.Count > 0)
            {
                foreach (XmlNode nd in nodeList)
                {
                    DataSetType a = new DataSetType(nd, nsmgr);
                    if (a != null && _dataSetList.Exist(a) == false)
                        _dataSetList.Add(a);
                }

                found = true;
            }
            return found;
        }

        public override void WriteXml(XmlTextWriter xw, ISdmxArtefactRef selectedArtefact)
        {
            if (_dataSetList.Count > 0)
            {
                string prefix = "generic";

                foreach (DataSetType m in _dataSetList)
                {
                    if (selectedArtefact == null || m == selectedArtefact)
                        m.WriteXml(xw, prefix, "DataSet");
                }
            }
        }

        public override TreeNode FillNode(TreeNode parentNode, int startIndex)
        {
            TreeNode createdNode = null;
            // already attached to parent, Bind List of artefacts
            for (int i = startIndex; i < _dataSetList.Count; i++)
                createdNode = _dataSetList[i].FillNode(parentNode);
            return createdNode;
        }


        public override void DelChild(object o)
        {
            DataSetType a = o as DataSetType;
            if (a != null && _dataSetList.Contains(a))
                _dataSetList.Remove(a);
        }

        public override TreeNode NewChild(TreeNode node)
        {
            DataSetType a = new DataSetType();
            _dataSetList.Add(a);
            TreeNode t = a.FillNode(node);
            return t;
        }


    }
}
